﻿package net.pezzato.graphics{
	import flash.display.Sprite;
	import flash.text.TextFormat;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.events.Event;
	public class Widget extends Sprite {
		private var type_:String;
		private var min_:Point;
		private var max_:Point;
		private var widgetParent_:Widget;
		private var recalculatingDimension_:Boolean;
		private var rect_:Rectangle;
		private var floating_:Boolean;
		private static var defaultTextFormat_;
		protected var dispatchChange_:Boolean;
		public function Widget(widgetParent:Widget, type = "Unknown Widget type") {
			widgetParent_ = widgetParent;
			if (widgetParent_ != null) {
				widgetParent_.addChild(this);
			}
			type_ = type;
			rect_ = new Rectangle();
			recalculatingDimension_ = false;
			dispatchChange_ = true;
			floating = false;
		}
		public function get widgetType():String {
			return type_;
		}
		public function set widgetType(value:String):void {
			type_ = value;
		}
		public function set floating(value:Boolean) {
			floating_ = value;
		}
		public function get floating() {
			return floating_;
		}
		public static function get defaultTf() {
			if (defaultTextFormat_ == null) {
				defaultTextFormat_ = new TextFormat();
				defaultTextFormat_.font = "Verdana";
				defaultTextFormat_.size = 11;
			}
			return defaultTextFormat_;
		}
		protected function toForeground():void {
			if (parent is DisplayObjectContainer) {
				this.parent.setChildIndex(this, this.parent.numChildren - 1);
			}
		}
		protected function maximize() {
			if (parent != null) {
				x = 0;
				y = 0;
				width = parent.width;
				height = parent.height;
			}
		}
		protected function fitContent() {
			for (var i:Number = 0; i < numChildren; ++i) {
				var c:DisplayObject = getChildAt(i);
				if (c.x < rect_.x) {
					rect_.x = c.x;
				}
				if (c.y < rect_.y) {
					rect_.y = c.y;
				}
				if (c.x + c.width > rect_.width + rect_.x) {
					rect_.width = c.x + c.width - rect_.x;
				}
				if (c.y + c.height > rect_.height + rect_.y) {
					rect_.y = c.y + c.height - rect_.y;
				}
			}
			changed();
		}
		protected function move(a:DisplayObject, way:String, b:DisplayObject, margin:Number) {
			if (way == "leftOf") {
				a.x = b.x + b.width + margin;
				a.y = b.y;
			}
		}
		protected function get rect() {
			return rect_;
		}
		protected function changed() {
			if (dispatchChange_) {
				onChange();
				if (widgetParent_) {
					widgetParent_.childChanged(this);
				}
			}
		}
		protected function onChange() {
		}
		private function childChanged(child:Widget) {
			recalculateDimension();
		}
		private function recalculateDimension() {
			if (!recalculatingDimension_) {
				recalculatingDimension_ = true;
				for (var i:Number = 0; i < numChildren; ++i) {
					var c:DisplayObject = getChildAt(i);
					if (c.x < rect_.x) {
						rect_.x = c.x;
					}
					if (c.y < rect_.y) {
						rect_.y = c.y;
					}
					if (c.x + c.width > rect_.width + rect_.x) {
						rect_.width = c.x + c.width - rect_.x;
					}
					if (c.y + c.height > rect_.height + rect_.y) {
						rect_.y = c.y + c.height - rect_.y;
					}
				}
				dispatchChange_ = false;
				onChange();
				dispatchChange_ = true;
				recalculatingDimension_ = false;
			}
		}
	}
}